#ifndef CDPLAYER_H
#define CDPLAYER_H
#include "sumHeader.h"


struct CCVert{
    float x;
    float y;
    float z;
    float u;
    float v;
};



struct MyPoint{
    MyPoint(double x, double y)
        :_x(x)
         ,_y(y)
    {

    }
    MyPoint(const QPoint &p)
    {
        _x = p.x();
        _y = p.y();
    }
    static MyPoint ToMyPoint(const QPoint &p)
    {
        MyPoint tempP(p);
        return  tempP;
    }

    double x() const
    {
        return _x;
    }
    double y() const
    {
        return _y;
    }
    MyPoint()
    {

    }
    QPoint toQPoint()
    {
        return QPoint(_x, _y);
    }
    void setPoint(double x, double y)
    {
        _x = x;
        _y = y;
    }

    MyPoint operator-(const MyPoint &temp)
    {
        MyPoint temp2;
        temp2.setPoint( _x - temp._x, _y - temp._y);

        return temp2;
    }
    MyPoint operator-(const QPoint &temp)
    {
        MyPoint temp2;
        temp2.setPoint( _x - temp.x(), _y - temp.y());

        return temp2;
    }
    MyPoint operator+(const MyPoint &temp)
    {
        MyPoint temp2;
        temp2.setPoint( _x + temp._x, _y + temp._y);

        return temp2;
    }

    double _x = 0;
    double _y = 0;
};


class CdPlayer:public QOpenGLWidget, public QOpenGLExtraFunctions
{
    Q_OBJECT
public:
    CdPlayer(QOpenGLWidget *parent = nullptr);
    ~CdPlayer();
    void initPoint(QPoint centralPoint, QPoint cdArmPos);
public:
    constexpr static double pi = 3.1415926;
    constexpr static int Height = 510;

public:
    double GetAngle(const QPoint &vec1, const QPoint & vec2);
    double GetAngle(const MyPoint &vec1, const MyPoint & vec2);
    int AdvanceOrRetreat(const QPoint &dot, const QPoint &dot2);
    int CrossProduct(const QPoint &vec1, const QPoint &vec2);
    void errPrint(QString InforMation);

private slots:
    //参数是一个进度占比
    void acceptMusicProgress(double rotia);
    void acceptStart();
    void acceptResetBegin();
//    void acceptTimeUpdate(double t0);
//    double qDegreesToRadians(double rad);
signals:
    //参数是一个进度占比
//    void musicProgress(double rotia);
    void sendMusicProgress(double rotia);
    void sendVlcProgress(double rotia);
private:
//    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    bool locateSqureAreaInternal(QPoint);
    void updateCurrentPoint(double RadianAdded);
    void updateRadian();
    void resetMin();
    void resetMax();
    void paintGL() override;
    void initializeGL() override;
    void resizeGL(int w, int h) override;
private:
    QImage      _picture, _picture2;
    GLuint      _cdTex = 0, _armTex = 0;
private:

    bool _isOrNoInRectArea;//判断是否点中cd的机器臂头

    QPoint _centralPoint;//cd中心点
    int _centralRoundRadius;//圆半径
    QPoint _cdArmPos;//cd的机器臂位置
    MyPoint _squareA;
    MyPoint _squareB;
    MyPoint _squareC;
    MyPoint _squareD;

    MyPoint _squareCurA,_squareCurB,_squareCurC,_squareCurD;
    MyPoint _squareMaxA,_squareMaxB,_squareMaxC,_squareMaxD;
    double _currentRotationAngle = 0.00;//当前旋转角度
    double _maxRotationAngle;//最大旋转角度

    double _lengthA, _lenthB, _lenthC, _lenthD;

//    double _curLengthA, _curLenthB, _curLenthC, _curLenthD;

    double      _angleAOrg  =  15.0f * pi / 180;//该变量得出的是弧度
    double      _angleBOrg  =  11.0f * pi / 180;
    double      _angleCOrg  =   6.0f * pi / 180;
    double      _angleDOrg  =  11.0f * pi / 180;

    double      _angleACur  =  15.0f * pi / 180;
    double      _angleBCur  =  11.0f * pi / 180;
    double      _angleCCur  =   6.0f * pi / 180;
    double      _angleDCur  =  11.0f * pi / 180;

    double      _angleAMax;
    double      _angleBMax;
    double      _angleCMax;
    double      _angleDMax;

    double      _cdAngleCur;
    double      _cdAngleMax;
    double      _cdAngleMin;

    QPoint _lastMousePos;
    double _currentMinDistance;//当前圆心到AD向量的最短距离
};

#endif // CDPLAYER_H
